package com.qxsx.midi.activity.exam;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.ExamMyItemAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.Exam;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zrj on 2015/9/10.
 * 我的考试
 */
public class ExamMyListActivity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private ListView lv_mylist;
    private ExamMyItemAdapter adapter;
    private List<Exam> list;
    private String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_mylist);
        setTitle("我的考试");
        setNavBtn(R.mipmap.back, 0);
        user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        findById();

    }

    private void findById() {
        lv_mylist = (ListView) findViewById(R.id.exam_list);
    }

    private void inItData() {
        list = new ArrayList<Exam>();
        adapter = new ExamMyItemAdapter(AppContext.getContext(), list,ExamMyListActivity.this);
        lv_mylist.setAdapter(adapter);


        if (TextUtils.isEmpty(user_id)) {
            Toast.makeText(ExamMyListActivity.this, "你还未登录，请先登录", Toast.LENGTH_SHORT).show();
            return;
        } else {
            customProgress = CustomProgress.show(ExamMyListActivity.this, "请稍后....", true, null);

            JSONArray array = new JSONArray();
            JSONObject object = new JSONObject();
            try {
                object.put("user_id", user_id);
                array.put(object);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            MyNet.doPostJsonArray("8011", array, new callBackJsonArray() {
                @Override
                public void run(Object code, Object message, JSONArray array) {
                    if ("200".equals(code.toString())) {
                        list = new ArrayList<Exam>();
                        try {
                            for (int i = 0; i < array.length(); i++) {
                                Exam exam = new Exam();
                                JSONObject object = array.getJSONObject(i);
                                exam.set_id(object.getString("id"));
                                exam.set_name(object.getString("major_name"));

                                String major_id = object.getString("major_id");
                                if (major_id.toString().equals("2172405d-17bb-5f40-da92-cf9cc285a980")) {
                                    exam.set_url(R.mipmap.exam_wait_guitar);
                                } else if (major_id.toString().equals("cd12a552-95ee-9d9b-1efb-8c302c6b52c2")) {
                                    exam.set_url(R.mipmap.exam_wait_jazz);
                                } else if (major_id.toString().equals("5bb9bcb5-88a0-9c85-0a15-39b51c1142be")) {
                                    exam.set_url(R.mipmap.exam_wait_bass);
                                } else if (major_id.toString().equals("87b65e6a-72fc-4617-d61b-351a7d080ed7")) {
                                    exam.set_url(R.mipmap.exam_wait_base);
                                }
                                exam.set_grade(object.getString("grade_num"));
                                exam.set_addr(object.getString("exam_site_name"));
                                exam.set_timer(object.getString("exam_time"));
                                exam.set_code(object.getString("state_code"));
                                exam.set_mesg(object.getString("state_msg"));
                                list.add(exam);
                            }
                            adapter.AddData(list);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", message.toString());
                    }
                    customProgress.dismiss();
                }
            });


        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        inItData();
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }
}
